#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

DIR=$PWD
WORK_ROOT=$PWD

function prepare_env() {
    export JAVA_HOME=$DIR/flutter_env/jdk-17.0.8

    export PUB_HOSTED_URL=https://pub.flutter-io.cn
    export FLUTTER_STORAGE_BASE_URL=https://flutter-ohos.obs.cn-south-1.myhuaweicloud.com

    export FLUTTER_HOME=$DIR/flutter_flutter
    export PATH=$JAVA_HOME/bin:$FLUTTER_HOME/bin:$PATH

    export TOOL_HOME=$DIR/command-line-tools
    export DEVECO_SDK_HOME=$TOOL_HOME/sdk
    export NODE_HOME=$DIR/flutter_env/node-v16.14.2-linux-x64
    export PATH=$TOOL_HOME/ohpm/bin:$TOOL_HOME/hvigor/bin:$NODE_HOME/bin:$PATH
}

function download_diff() {
    local pr_num=$1
    local token="dwCzGQf1Wy9oKTfvezoT7C7x"
    local url="https://api.gitcode.com/api/v5/repos/openharmony-sig/flutter_multidevice_layout_scenepkg/pulls/$pr_num/diff"

    curl --location \
    $url \
    --header 'PRIVATE-TOKEN: dwCzGQf1Wy9oKTfvezoT7C7x' \
    -o $pr_num.diff

    if [ $? -ne 0 ]
    then
        echo "curl failed!"
        return 1
    fi
    return 0
}

function get_project_name() {
    DEFALUT_PROJECT='packages/multidevice_layout'
    local diff_url=$1
    local pr_num=${diff_url##*/}
    local diff_name=$pr_num.diff
    local retval=1
    local compont_name=
    if [ ! -f $diff_name ]
    then
        download_diff $pr_num
        if [ $? -ne 0 ]
        then
            echo "download $diff_name failed!"
            return $retval
        fi
    fi
    local diff_str=`cat $diff_name | grep "diff --git"`

    for diff in $diff_str
    do
        local tmp=`echo $diff | grep "/"`
        if [ ! -z "$tmp" ]
        then
            local find=0
            readarray -t dir_array <<< "$(echo "$tmp" | tr '/' '\n')"
            length=${#dir_array[@]}
            if (( length >= 4 )) && [[ "${dir_array[1]}" != "example" ]]; then
                echo "${dir_array[1]}/${dir_array[2]}"
                retval=0
                find=1
                break
            fi
        fi
    done
    if [[ $find -eq 0 ]]
    then
        echo "$DEFALUT_PROJECT"
    fi
    return $retval
}
PROJECT=$(get_project_name $1)

function start_compile() {
    #配置环境变量
    prepare_env

    #执行构建
    echo "项目位置：$PROJECT"
    echo "PATH============== $PATH"
    echo "HOS_SDK_HOME============== $HOS_SDK_HOME"
    echo "NODE: $(node --version), $(which node)"
    ls -alh $TOOL_HOME/tool/node/bin/node
    echo "OHPM: $(ohpm --version) $(which ohpm)"
    ls -alh $TOOL_HOME/ohpm/bin/ohpm

    #SDK准备
    cicd/flutter/engine/prepare_sdk.sh

    flutter doctor -v
    cd "./flutter/$PROJECT/example" && flutter pub get && rm -rf ./pubspec.lock
    flutter build hap --target-platform ohos-arm64 --debug
    echo $DIR
    # mv ./ohos/entry/build/default/outputs/default/entry-default-unsigned.hap $DIR/hap_out

    #return 1
    return 0
}

# shellcheck disable=SC2068
start_compile $@

exit $?

#eof